//
//  SHPickerView.m
//  Soha
//
//  Created by 谭飞 on 2018/8/31.
//  Copyright © 2018年 LB. All rights reserved.
//

#import "SHPickerView.h"
#import "UIView+AddGesture.h"

@interface SHPickerView ()
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIButton *cancleButton;
@property (nonatomic, strong) UIButton *sureButton;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) NSInteger selectRow;

@end

@implementation SHPickerView

#pragma mark - Initializer

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {

    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title items:(NSArray *)items block:(void (^)(SHPickerView *, NSInteger))block  {
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        _title = title;
        _items = items;
        _sureCallback = block;

        _titleHeight = 44.f;
        _contentHeight = 216.f;
        [self setupSubViews];
        
        [_containerView addSubview:self.pickerView];
        [[UIPickerView appearance] setBackgroundColor:kCommonBackgroudColor];
       
    }
    return self;
}

- (instancetype) initDatePickerWithTitle:(NSString *)title block:(void (^)(NSDate *))block {
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        _title = title;
        _handleBlock = block;
        
        _titleHeight = 44.f;
        _contentHeight = 216.f;
        
        [self setupSubViews];
        [[UIDatePicker appearance] setBackgroundColor:kCommonBackgroudColor];
        [_containerView addSubview:self.datePicker];
    }
    return self;
}

- (void)setupSubViews {
    
    _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
    _backgroundView.backgroundColor = [UIColor blackColor];
    [self addSubview:_backgroundView];
    
    _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.xmg_height, self.xmg_width, _titleHeight + _contentHeight + SAFEAREAHEIGHT)];
    _containerView.backgroundColor = COLOR_HEX(0xf2f2f2);
    [self addSubview:_containerView];
    
    _cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancleButton.titleLabel.font = WSFontMake(15);
    [_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancleButton setTitleColor:kThemeGreenColor forState:UIControlStateNormal];
    [_cancleButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:_cancleButton];
    
    [_cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self->_containerView);
        make.size.mas_equalTo(CGSizeMake(60, self->_titleHeight));
    }];
    
    _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureButton.titleLabel.font = WSFontMake(15);
    [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [_sureButton setTitleColor:kThemeGreenColor forState:UIControlStateNormal];
    [_sureButton addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:_sureButton];
    
    [_sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self->_containerView);
        make.size.mas_equalTo(CGSizeMake(60, self->_titleHeight));
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.text = _title;
    _titleLabel.font = WSFontMake(16);
    _titleLabel.textColor = kFontColorGray;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [_containerView addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_containerView);
        make.left.equalTo(self->_cancleButton.mas_right);
        make.right.equalTo(self->_sureButton.mas_left);
        make.height.equalTo(self->_cancleButton);
    }];
    
    
    WEAKSELF
    [_backgroundView addGestureWithGestureType:WJ_TapGesture completeHandler:^{
        [weakSelf dismiss];
    }];
    
}

- (void)show {
    [KAppWindow endEditing:YES];
    _backgroundView.alpha = 0;
    _containerView.xmg_y = self.xmg_height;
    [KAppWindow addSubview:self];
    [UIView animateWithDuration:0.25f animations:^{
        self->_backgroundView.alpha = 0.4;
        self->_containerView.xmg_bottom = self.xmg_height;
    }];
}

#pragma mark - Event Response

// 点击确定
- (void)sureAction {
    if (self.sureCallback) {
        self.sureCallback(self, _selectRow);
    }
    if (self.handleBlock) {
        self.handleBlock(_selectDate);
    }
    [self dismiss];
}


- (void)dateChanged {
    _selectDate = self.datePicker.date;
}


- (void)dismiss {
    [UIView animateWithDuration:0.25 animations:^{
        _backgroundView.alpha = 0;
        _containerView.xmg_y = self.xmg_height;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}


#pragma mark - UIPickerViewDataSource, UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _items.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return _items[row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *contentLabel = (UILabel *)view;
    @autoreleasepool {
        if (!contentLabel) {
            contentLabel = [[UILabel alloc] init];
            contentLabel.font = WSFontMake(18);
            contentLabel.textColor = kFontColorBlack;
            contentLabel.textAlignment = NSTextAlignmentCenter;
        }
    }
    contentLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return contentLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _selectRow = row;
}


- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 80.f;
}

#pragma mark - lazy load

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, _titleHeight, kMainScreenWidth, _contentHeight)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}

- (UIDatePicker *)datePicker {
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, _titleHeight, kMainScreenWidth, _contentHeight)];
        //设置地区: zh-中国
        _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        [_datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
    }
    return _datePicker;
}


@end
